package minijava;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import minijava.parser.jcc.JCCMiniJavaParser;
import minijava.parser.jcc.ParseException;
import minijava.symtable.GlobalTable;
import minijava.visitor.SymbolTableBuilder;
import minijava.visitor.TypeCheckVisitor;
import minijava.visitor.TypeCheckerVisitor;
import minijava.ast.Program;

public class MiniJava {

	public static void main(String[] args) throws IOException {
		//runPrompt();
		runFile("test1.jav");
	}

	private static void runFile(String path) throws IOException {
		//byte[] bytes = Files.readAllBytes(Paths.get(path));
		run(new FileInputStream(path));
	}

	private static void runPrompt() throws IOException {
		for (;;) {
			System.out.print("> ");
			run(System.in);
		}
	}

	private static void run(InputStream input) {
		try {
			Program program = new JCCMiniJavaParser(input).Program();
			System.out.println(program);
			
			SymbolTableBuilder vb = new SymbolTableBuilder();
			vb.visit(program);
			GlobalTable globalTable = vb.getTable();
			
			TypeCheckVisitor tcv = new TypeCheckVisitor(globalTable);
			
			System.out.println(globalTable);
			tcv.visit(program);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}
